package com.oversea.action;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.oversea.util.Constants;
import com.oversea.util.page.Page;
import com.oversea.util.searchcontext.SearchContext;

public class BaseAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 244538118277633938L;

	private static final String BRANDING_PATH = "display";

	protected String contextPath;

	private String displayStaticLocation;

	protected int currentPage = 1;

	protected int pageSize = 8;

	private String menuname;

	private boolean opResult;

	protected Page page;

	private String opMessage;
	
	private int navigatioinIndex=1; //控制前台页面显示哪个导航菜单选中
	
	public String getDisplayStaticLocation() {
		StringBuffer temp = new StringBuffer(
				getContextPath(ServletActionContext.getRequest())).append("/")
				.append(BRANDING_PATH).append("/");
		displayStaticLocation = temp.toString();
		return displayStaticLocation;
	}

	public void setDisplayStaticLocation(String brandingStaticLocation) {
		this.displayStaticLocation = brandingStaticLocation;
	}

	public String getContextPath(HttpServletRequest request) {
		return request == null ? "" : request.getContextPath();
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getMenuname() {
		return menuname;
	}

	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}

	public boolean isOpResult() {
		return opResult;
	}

	public void setOpResult(boolean opResult) {
		this.opResult = opResult;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getOpMessage() {
		return opMessage;
	}

	public void setOpMessage(String opMessage) {
		this.opMessage = opMessage;
	}

	public String getLoginUserName() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session == null
				|| session.get(Constants.LOGIN_USER_NAME_KEY) == null) {
			return "";
		}
		return (String) session.get(Constants.LOGIN_USER_NAME_KEY);
	}

	public Integer getLoginUserId() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session == null || session.get(Constants.LOGIN_USER_ID_KEY) == null) {
			return null;
		}
		return (Integer) session.get(Constants.LOGIN_USER_ID_KEY);
	}

	public boolean hasPermission(int level) {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session == null) {
			return false;
		}
		Integer userLevel = (Integer) session
				.get(Constants.LOGIN_USER_ROLE_LEVEL_KEY);
		if (userLevel == null) {
			return false;
		}
		return userLevel >= level;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}

	public Integer getLoginUserLevel() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session == null) {
			return null;
		}
		Integer userLevel = (Integer) session
				.get(Constants.LOGIN_USER_ROLE_LEVEL_KEY);
		return userLevel;
	}

	public int getNavigatioinIndex() {
		return navigatioinIndex;
	}

	public void setNavigatioinIndex(int navigatioinIndex) {
		this.navigatioinIndex = navigatioinIndex;
	}

}
